package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.asn1.axdr.AxdrNull;
import com.dl698.asn1.axdr.AxdrEnum;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * ConnectMechanismInfoChoice枚举
 * 定义连接机制信息选择的枚举值
 */
enum ConnectMechanismInfoChoice {
    _ERR_NONE_SELECTED(-1),
    NULL_SECURITY(0),
    PASSWORD_SECURITY(1),
    SYMMETRY_SECURITY(2),
    SIGNATURE_SECURITY(3);

    private final int value;

    ConnectMechanismInfoChoice(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public static ConnectMechanismInfoChoice fromValue(int value) {
        for (ConnectMechanismInfoChoice choice : ConnectMechanismInfoChoice.values()) {
            if (choice.value == value) {
                return choice;
            }
        }
        return _ERR_NONE_SELECTED;
    }
}

/**
 * ConnectMechanismInfo类
 * 实现连接机制信息的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class ConnectMechanismInfo implements AxdrType {

    private byte[] dataCode = null;
    private ConnectMechanismInfoChoice choice = ConnectMechanismInfoChoice._ERR_NONE_SELECTED;
    private AxdrNull nullSecurity = null;
    private PasswordSecurity passwordSecurity = null;
    private SymmetrySecurity symmetrySecurity = null;
    private SignatureSecurity signatureSecurity = null;

    public ConnectMechanismInfo() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setNullSecurity(AxdrNull nullSecurity) {
        this.resetChoices();
        this.choice = ConnectMechanismInfoChoice.NULL_SECURITY;
        this.nullSecurity = nullSecurity;
    }

    public void setPasswordSecurity(PasswordSecurity passwordSecurity) {
        this.resetChoices();
        this.choice = ConnectMechanismInfoChoice.PASSWORD_SECURITY;
        this.passwordSecurity = passwordSecurity;
    }

    public void setSymmetrySecurity(SymmetrySecurity symmetrySecurity) {
        this.resetChoices();
        this.choice = ConnectMechanismInfoChoice.SYMMETRY_SECURITY;
        this.symmetrySecurity = symmetrySecurity;
    }

    public void setSignatureSecurity(SignatureSecurity signatureSecurity) {
        this.resetChoices();
        this.choice = ConnectMechanismInfoChoice.SIGNATURE_SECURITY;
        this.signatureSecurity = signatureSecurity;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        choosen.setConst();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = ConnectMechanismInfoChoice.fromValue((int) choosen.getValue());

        if (this.choice == ConnectMechanismInfoChoice.NULL_SECURITY) {
            this.nullSecurity = new AxdrNull();
            codeLength += this.nullSecurity.decode(input);
            return codeLength;
        } else if (this.choice == ConnectMechanismInfoChoice.PASSWORD_SECURITY) {
            this.passwordSecurity = new PasswordSecurity();
            codeLength += this.passwordSecurity.decode(input);
            return codeLength;
        } else if (this.choice == ConnectMechanismInfoChoice.SYMMETRY_SECURITY) {
            this.symmetrySecurity = new SymmetrySecurity();
            codeLength += this.symmetrySecurity.decode(input);
            return codeLength;
        } else if (this.choice == ConnectMechanismInfoChoice.SIGNATURE_SECURITY) {
            this.signatureSecurity = new SignatureSecurity();
            codeLength += this.signatureSecurity.decode(input);
            return codeLength;
        }
        throw new RuntimeException("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        if (this.dataCode != null) {
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(this.dataCode[i]);
                } catch (java.io.IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return this.dataCode.length;
        }
        if (this.choice == ConnectMechanismInfoChoice._ERR_NONE_SELECTED) {
            throw new RuntimeException("Error encoding AxdrChoice: No item in choice was selected.");
        }

        int codeLength = 0;

        if (this.choice == ConnectMechanismInfoChoice.SIGNATURE_SECURITY) {
            if (this.signatureSecurity != null) {
                codeLength += this.signatureSecurity.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(3);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("signatureSecurity is null");
            }
        } else if (this.choice == ConnectMechanismInfoChoice.SYMMETRY_SECURITY) {
            if (this.symmetrySecurity != null) {
                codeLength += this.symmetrySecurity.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(2);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("symmetrySecurity is null");
            }
        } else if (this.choice == ConnectMechanismInfoChoice.PASSWORD_SECURITY) {
            if (this.passwordSecurity != null) {
                codeLength += this.passwordSecurity.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(1);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("passwordSecurity is null");
            }
        } else if (this.choice == ConnectMechanismInfoChoice.NULL_SECURITY) {
            if (this.nullSecurity != null) {
                codeLength += this.nullSecurity.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(0);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("nullSecurity is null");
            }
        }
        throw new RuntimeException("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            this.encode(revOStream);
        } catch (RuntimeException e) {
            // Handle exception
        }
        this.dataCode = revOStream.getArray();
    }

    public ConnectMechanismInfoChoice getChoiceIndex() {
        return this.choice;
    }

    public void resetChoices() {
        this.choice = ConnectMechanismInfoChoice._ERR_NONE_SELECTED;
        this.nullSecurity = null;
        this.passwordSecurity = null;
        this.symmetrySecurity = null;
        this.signatureSecurity = null;
    }

    @Override
    public String toString() {
        if (this.choice == ConnectMechanismInfoChoice.NULL_SECURITY) {
            if (this.nullSecurity != null) {
                return "choice: {nullSecurity: " + this.nullSecurity + "}";
            } else {
                return "choice is nullSecurity but nullSecurity is null";
            }
        } else if (this.choice == ConnectMechanismInfoChoice.PASSWORD_SECURITY) {
            if (this.passwordSecurity != null) {
                return "choice: {passwordSecurity: " + this.passwordSecurity + "}";
            } else {
                return "choice is passwordSecurity but passwordSecurity is null";
            }
        } else if (this.choice == ConnectMechanismInfoChoice.SYMMETRY_SECURITY) {
            if (this.symmetrySecurity != null) {
                return "choice: {symmetrySecurity: " + this.symmetrySecurity + "}";
            } else {
                return "choice is symmetrySecurity but symmetrySecurity is null";
            }
        } else if (this.choice == ConnectMechanismInfoChoice.SIGNATURE_SECURITY) {
            if (this.signatureSecurity != null) {
                return "choice: {signatureSecurity: " + this.signatureSecurity + "}";
            } else {
                return "choice is signatureSecurity but signatureSecurity is null";
            }
        } else {
            return "unknown";
        }
    }

    public AxdrNull getNullSecurity() {
        return nullSecurity;
    }

    public PasswordSecurity getPasswordSecurity() {
        return passwordSecurity;
    }

    public SymmetrySecurity getSymmetrySecurity() {
        return symmetrySecurity;
    }

    public SignatureSecurity getSignatureSecurity() {
        return signatureSecurity;
    }
}